import  _thread
import time
import datetime
import sys
import tkinter as tk


class Didaclass(object):
    """"""
    closecount = 0
    warnhidecount = 0

    def __init__(self, parent, showTime, hideTime):
        self.start = datetime.datetime.now()
        self.now = self.start
        self.font = ("黑体", 30, "bold")
        self.type = {True:showTime, False:hideTime}
        self.lists = [[True,0.05],[False,self.type[False]],[True,self.type[True]]]

        self.root = parent
        self.iniGui()
        self.root.overrideredirect(True)
        self.root.wm_state('zoomed')
        self.root['width'] = root.winfo_screenwidth()
        self.root['height'] = root.winfo_screenheight()
        self.root.attributes("-topmost", True)
        self.root.attributes("-alpha", 0.6)
        self.root.protocol('WM_DELETE_WINDOW', self.closeWindow)

        _thread.start_new_thread(self.showNow, ())
        _thread.start_new_thread(self.showNext, ())
        self.root.mainloop()
    # ----------------------------------------------------------------------
    def showNow(self):
        while True:
            time.sleep(1)
            self.timeStr.set("当前时间：{}".format(datetime.datetime.now()))

    def showNext(self):
        while True:
            time.sleep(1)
            self.nextStr.set("隐藏时间:{}".format(self.now + datetime.timedelta(minutes= self.lists[0][1])))
            self.nextShowStr.set("下次显示时间:{}分钟后".format(self.now + datetime.timedelta(minutes= self.lists[1][1])))
            if self.now + datetime.timedelta(minutes= self.lists[0][1]) < datetime.datetime.now():
                if not self.lists[1][0]:
                    self.hide()
                    self.lists = self.lists[1:] + [[False,self.type[False]]]
                    self.now = datetime.datetime.now()
                else:
                    self.lists = self.lists[1:] + [[True, self.type[True]]]
                    self.now = datetime.datetime.now()
                    self.show()
            if self.warnhidecount > 0:
                self.warnhidecount = self.warnhidecount -1
            elif self.warnhidecount == 0:
                self.warnStr.set("")
                self.warnhidecount = -1

    def iniGui(self):
        self.timeStr = tk.StringVar(self.root,"");
        self.timelabel = tk.Label(self.root,textvariable = self.timeStr ,font=self.font)
        self.timelabel.pack(anchor=tk.CENTER, expand=tk.YES, fill=tk.X)

        self.nextStr = tk.StringVar(self.root, "")
        self.nextLabel = tk.Label(self.root, textvariable=self.nextStr, font=self.font)
        self.nextLabel.pack(anchor=tk.CENTER, expand=tk.YES, fill=tk.X)

        self.nextShowStr = tk.StringVar(self.root, "")
        self.nextShowLabel = tk.Label(self.root, textvariable=self.nextShowStr, font=self.font)
        self.nextShowLabel.pack(anchor=tk.CENTER, expand=tk.YES, fill=tk.X)

        self.warnStr = tk.StringVar(self.root, "");
        self.warnlabel = tk.Label(self.root, textvariable = self.warnStr, font = self.font,fg ='#FF0000')
        self.warnlabel.pack(anchor=tk.CENTER, expand=tk.YES, fill=tk.X)

    def closeWindow(self):
        self.closecount = self.closecount + 1
        if self.closecount < 2:
            self.warnStr.set("再按一次重新计时")
            self.warnhidecount = 3
        else:
            self.hide()
            self.closecount = 0
            self.lists = self.lists[1:] + [[False, self.type[False]]]
            self.now = datetime.datetime.now()
            self.warnStr.set("")
        return

    def hide(self):
        """"""
        self.root.withdraw()

    # ----------------------------------------------------------------------


    # ----------------------------------------------------------------------
    def show(self):
        """"""
        self.root.update()
        self.root.deiconify()
        self.root.wm_state('zoomed')
        self.root['width'] = root.winfo_screenwidth()
        self.root['height'] = root.winfo_screenheight()
        self.root.attributes("-topmost", True)

if __name__ == "__main__":
    start = datetime.datetime.now()
    print(sys.argv)
    showTime = 15
    hideTime = 45
    if len(sys.argv) > 1:
        showTime = sys.argv[1]
    elif len(sys.argv) == 3:
        hideTime = sys.argv[2]

    root = tk.Tk()
    app = Didaclass(root, showTime, hideTime)



    # w, h = root.maxsize()
    # root.geometry("{}x{}".format(w, h))
    # 进入消息循环
    # root.mainloop()

